#include "Orion20m.h"
#include "cvel.h"

int ORION::clbkConsumeBufferedKey(DWORD key, bool down, char *kstate) {
	if (!down) return 0;

	if(cfg->hudmode==HUD_EVA) {
		if ( !KEYMOD_SHIFT (kstate) && !KEYMOD_CONTROL (kstate) && !KEYMOD_ALT (kstate) ) {
			int ret=0;
			switch(key) {
				case OAPI_KEY_1:
					ummu_selectmember(SelectedUmmuMember-1);
				return 1;
				case OAPI_KEY_2:
					ummu_selectmember(SelectedUmmuMember+1);
				return 1;
				case OAPI_KEY_3:
					ummu_selectdock(SelectedUmmuDock-1);
				return 1;
				case OAPI_KEY_4:
					ummu_selectdock(SelectedUmmuDock+1);
				return 1;
				case OAPI_KEY_E:
					ummuret(Crew.EvaCrewMember(Crew.GetCrewNameBySlotNumber(SelectedUmmuMember)));
				return 1;
				case OAPI_KEY_M:
					AddUMmuToVessel(true);
				return 1;
			}
		} else if ( !KEYMOD_SHIFT (kstate) && !KEYMOD_CONTROL (kstate) && KEYMOD_ALT (kstate) ) {
			switch (key) {
				case OAPI_KEY_J:
					ummu_selectdock(SelectedUmmuDock+1);
				return 1;
				case OAPI_KEY_K:
					ummu_selectdock(SelectedUmmuDock-1);
				return 1;
			}
		}
	} else if(cfg->hudmode==HUD_RCS) {
		if ( !KEYMOD_SHIFT (kstate) && !KEYMOD_CONTROL (kstate) && !KEYMOD_ALT (kstate) ) {
			switch (key) {
				case OAPI_KEY_K:
					rcs_shift_mode=1;
				return 1;
				case OAPI_KEY_G:
/*					SSCOUNT=0;
					ssmass=0.0;
					rot_comp_level=1.0;
					if(rot_comp_mode==0) {
						double vmass=GetMass();
						if(vmass==0) return 1;
						rot_comp_mode=1;
						VESSEL * s = oapiGetVesselInterface(GetHandle());
						getsuperstructuremass(s);
						sprintf(hudlastmsg,"Superstructure mass: %f - %f = %f",ssmass,vmass,(ssmass-vmass));
						dprint("Superstructure mass: %f - %f = %f",ssmass,vmass,(ssmass-vmass));
						rot_comp_level=ssmass/vmass;
					} else {
						rot_comp_mode=0;
					}
*/
				return 1;
			}
		}
	} 
#ifdef TESTING
	else if(cfg->hudmode==HUD_CAMERA) {
		if ( !KEYMOD_SHIFT (kstate) && !KEYMOD_CONTROL (kstate) && !KEYMOD_ALT (kstate) ) {
			if(oapiCameraInternal()) {
				switch (key) {
					case OAPI_KEY_1:
						chcam(0);
					return 1;
					case OAPI_KEY_2:
						chcam(cfg->cam+1);
					return 1;
					case OAPI_KEY_3:
						chcam(cfg->cam-1);
					return 1;
					case OAPI_KEY_MINUS:
						chcamapt(-1);
					return 1;
					case OAPI_KEY_EQUALS:
						chcamapt(1);
					return 1;
					case OAPI_KEY_H:
						chcamdir(1,-1);
					return 1;
					case OAPI_KEY_L:
						chcamdir(1,1);
					return 1;
					case OAPI_KEY_J:
						chcamdir(0,1);
					return 1;
					case OAPI_KEY_K:
						chcamdir(0,-1);
					return 1;
					case OAPI_KEY_7:
						cfg->camanimlink=(cfg->camanimlink==1?0:1);
					return 1;
					/*case OAPI_KEY_8:
						cfg->testcamx_mode=(cfg->testcamx_mode==0.0?-1.0:0.0);
					return 1;*/

					case OAPI_KEY_0:
						{
							VECTOR3 ndir;
							if(sscanf("0 0 1","%lf%lf%lf",&ndir.x,&ndir.y,&ndir.z)==3) {
								syslog("setdir: %f,%f,%f",ndir);
								SetCameraDefaultDirection(ndir);
								cameras[cfg->cam].dir=ndir;
								oapiCameraSetCockpitDir(0,0);
							}
						}
					return 1;
				}
			}
		}
	}
#endif
///// Global keys
	if ( !KEYMOD_SHIFT (kstate) && !KEYMOD_CONTROL (kstate) && !KEYMOD_ALT (kstate) ) {
		if(cfg->cam>0) {
			switch (key) {
				case OAPI_KEY_PRIOR:
					chcamapt(-1);
				return 1;
				case OAPI_KEY_NEXT:
					chcamapt(1);
				return 1;
				case OAPI_KEY_END:
					cfg->camyawmode=0.0;
					cfg->campitchmode=0.0;
					cameras[cfg->cam].yaw_proc=0.0;
					cameras[cfg->cam].pitch_proc=0.0;
					chcamdir(1,0,0.0);
				return 1;
			}
		}
		switch (key) {
			case OAPI_KEY_U:
				if(cfg->hudmode+1<HUDMENUCOUNT) {
					chhud(cfg->hudmode+1);
				} else {
					cfg->hudmode=HUD_OFF;
					sprintf(hudtitle,"[U]: Hud on.");
				}
			return 1;
			case OAPI_KEY_J:
				rcs_linshift( (cfg->rcs_cog_comp>0?0:1) );
				cfg->rcs_cog_comp=(cfg->rcs_cog_comp>0?0:1);
			return 1;
				case OAPI_KEY_L:
				docklight();
			return 1;
			case OAPI_KEY_5:
				chyield(0);
			return 1;
			case OAPI_KEY_6:
				chyield(1);
			return 1;
			case OAPI_KEY_HOME:
				chcam(0);
			return 1;
			case OAPI_KEY_INSERT:
				chcam(cfg->cam-1);
			return 1;
			case OAPI_KEY_DELETE:
				chcam(cfg->cam+1);
			return 1;
		}
	} else if ( !KEYMOD_SHIFT (kstate) && !KEYMOD_CONTROL (kstate) && KEYMOD_ALT (kstate) ) {
		switch (key) {
			case OAPI_KEY_G:
				{
				//VESSEL * s = oapiGetVesselInterface(GetHandle());
				//getsuperstructuremass(s);
				//syslog("Superstructure mass: %f",ssmass);
					//EulerToQuaternion(0.0,45.0,0);
				}
			return 1;
			case OAPI_KEY_A:
				armnuke(cfg->nukearmed?0:1);
			return 1;
			case OAPI_KEY_1:
				if(cfg->nukearmed==1 && cfg->nukesafety1==0) {
					syslog("ERROR: Disarm NPP engine before engaging safety.");
				} else {
					cfg->nukesafety1=(cfg->nukesafety1?0:1);
					syslog("Nuclear arming safety #1 now %s.",(cfg->nukesafety1?"enabled":"disabled"));
				}
			return 1;
			case OAPI_KEY_2:
				if(cfg->nukearmed==1 && cfg->nukesafety2==0) {
					syslog("ERROR: Disarm NPP engine before engaging safety.");
				} else {
					cfg->nukesafety2=(cfg->nukesafety2?0:1);
					syslog("Nuclear arming safety #2 now %s.",(cfg->nukesafety2?"enabled":"disabled"));
				}
			return 1;
			case OAPI_KEY_3:
				if(cfg->nukearmed==1 && cfg->nukesafety3==0) {
					syslog("ERROR: Disarm NPP engine before engaging safety.");
				} else {
					cfg->nukesafety3=(cfg->nukesafety3?0:1);
					syslog("Nuclear arming safety #3 now %s.",(cfg->nukesafety3?"enabled":"disabled"));
				}
			return 1;
			case OAPI_KEY_4:
				chyield(0);
			return 1;
			case OAPI_KEY_5:
				chyield(1);
			return 1;
		}
	} else if ( !KEYMOD_SHIFT (kstate) && KEYMOD_CONTROL (kstate) && !KEYMOD_ALT (kstate) ) {
		switch (key) {
			case OAPI_KEY_L:
				sprintf(oapiDebugString(), "");
				for(i=0;i<SYSLOGSIZE;i++) {
					shiplog[i].message[0]=0;
				}
				syslogp=0;
			return 1;
		}
	}
	return 0;
}
int ORION::clbkConsumeDirectKey (char *kstate) {
	if(cfg->cam!=0) {
		if ( !KEYMOD_SHIFT (kstate) && !KEYMOD_CONTROL (kstate) && !KEYMOD_ALT (kstate) ) {
			if  (KEYDOWN (kstate, OAPI_KEY_LEFT)) {
				cfg->camyawmode=-1.0;
				RESETKEY (kstate, OAPI_KEY_LEFT);
			} else if (KEYDOWN (kstate, OAPI_KEY_RIGHT)) {
				cfg->camyawmode=1.0;
				RESETKEY (kstate, OAPI_KEY_RIGHT);
			} else if (KEYDOWN (kstate, OAPI_KEY_UP)) {
				cfg->campitchmode=-1.0;
				RESETKEY (kstate, OAPI_KEY_UP);
			} else if (KEYDOWN (kstate, OAPI_KEY_DOWN)) {
				cfg->campitchmode=1.0;
				RESETKEY (kstate, OAPI_KEY_DOWN);
			} else {
				cfg->camyawmode=0.0;
				cfg->campitchmode=0.0;
			}
		} else {
			cfg->camyawmode=0.0;
			cfg->campitchmode=0.0;
		}
	}
	return 0;
}
